﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using CommonLibrary;
using CommonLibrary.Models.ControllerStates.PS3;
using InputManager.Helpers;
using InputManager.Models;

namespace InputManager.InputHandlers
{
    class FinalProcessor
    {
        private readonly IAppContext _context;
        private readonly PS3ControllerState _state;
        private readonly MouseTranslatorCalculator _mouseCalculat;

        public FinalProcessor(IAppContext context)
        {
            _context = context;
            _state = context.ControllerState as PS3ControllerState;
            _mouseCalculat = new MouseTranslatorCalculator();
        }

        // Events 

        private string action;

        public void KeyUp(Keys keyCode)
        {
            action = _context.ProfileConfig.ButtonActions.FirstOrDefault(d => d.Key.Equals(keyCode.ToString())).Value;
            if (action == null) return;

            _state.KeyCharToPS3CS(action, false);
        }

        public void KeyDown(Keys keyCode)
        {
            action = _context.ProfileConfig.ButtonActions.FirstOrDefault(d => d.Key.Equals(keyCode.ToString())).Value;
            if (action == null) return;

            _state.KeyCharToPS3CS(action, true);
        }

        public void KeyPressed(char keyChar)
        {
            // Not handeld - use key up/down for more key controll
        }

        // Mouse

        public void MouseWheel(int delta)
        {
            action = _context.ProfileConfig.ButtonActions.FirstOrDefault(d => d.Key.Equals(delta > 0 ? "M_WeelUp" : "M_WeelDown")).Value;
            if (action == null) return;

            _state.KeyCharToPS3CS(action, false);
        }

        public void MouseButtonUp(MouseButtons button)
        {
            action = _context.ProfileConfig.ButtonActions.FirstOrDefault(d => d.Key.Equals("M_" + button.ToString())).Value;
            if (action == null) return;

            _state.KeyCharToPS3CS(action, false);
        }

        public void MouseButtonDown(MouseButtons button)
        {
            action = _context.ProfileConfig.ButtonActions.FirstOrDefault(d => d.Key.Equals("M_" + button.ToString())).Value;
            if (action == null) return;

            _state.KeyCharToPS3CS(action, true);
        }

        AxisOffset point;

        public void MouseMoved(Point mousePosition)
        {
            var valx = _context.ProfileConfig.AxisAction.FirstOrDefault(a => a.Key.Equals("Mouse_Axis_X")).Value;
            var valy = _context.ProfileConfig.AxisAction.FirstOrDefault(a => a.Key.Equals("Mouse_Axis_Y")).Value;
            
            if (valx == null || valy == null) return;

            point = _mouseCalculat.CalculateValidMouseMovement(mousePosition, valx.DeadZone, valx.Exponent, valx.Multiplier, valy.DeadZone, valy.Exponent, valy.Multiplier, _context.GeneralInfo.NormalizeMouseInput);
            
            _state.MouseMovementToPS3CS(valx.ActionName, point.X);
            _state.MouseMovementToPS3CS(valy.ActionName, point.Y);
        }
    }
}
